package org.luxor.commons.sms.entity;

import org.luxor.commons.core.exception.BaseException;
import org.luxor.commons.core.utils.verify.TelUtils;
import org.luxor.commons.sms.constant.CountryCode;

import java.util.Objects;

/**
 * 国际手机号码
 *
 * @author Mr.Yan  @date: 2021/1/11
 */
public class PhoneNumber {

    /**
     * 国际区号
     */
    private CountryCode countryCode;
    /**
     * 手机号
     */
    private String mobile;

    /**
     * 国内手机号
     *
     * @param mobile 手机号码(不带国际区号,国内手机号微11位数值组成)
     */
    public PhoneNumber(String mobile) {
        if (!TelUtils.isPhone(mobile)) {
            throw new BaseException(40002, "手机号不合法", String.format("错误的手机号:[%s]", mobile));
        }
        this.countryCode = CountryCode.China;
        this.mobile = mobile;
    }

    /**
     * 国际手机号
     *
     * @param countryCode 国际区号
     * @param mobile      手机号码
     */
    public PhoneNumber(CountryCode countryCode, String mobile) {
        this.countryCode = countryCode;
        this.mobile = mobile;
    }

    public CountryCode getCountryCode() {
        return countryCode;
    }

    public void setCountryCode(CountryCode countryCode) {
        this.countryCode = countryCode;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    @Override
    public String toString() {
        return this.countryCode.getCode() + this.mobile;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        PhoneNumber that = (PhoneNumber) o;
        return countryCode == that.countryCode && Objects.equals(mobile, that.mobile);
    }

    @Override
    public int hashCode() {
        return Objects.hash(countryCode, mobile);
    }
}
